package com.vf.admin.client.dao;

import com.vf.admin.common.db.BaseDao;
import com.vf.admin.common.splitpage.SplitPageBean;
import com.vf.admin.miaodong.entity.AppDynamicDianzan;
import com.vf.admin.miaodong.entity.AppFollow;
import com.vf.admin.utils.StringEx;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 关注
 */
@Repository
public class AppFollowDao extends BaseDao<AppFollow> {

    /**
     * 查询列表
     */
    public List<AppFollow> list(Map<String,Object> paraMap, SplitPageBean splitPage) {
        ArrayList<Object> paramList = new ArrayList<Object>();
        StringBuilder sbStr = new StringBuilder("select * from app_follow where 1=1  ");
        if(StringEx.isNotNull(paraMap.get("startDate"))){
            sbStr.append(" and DATE_FORMAT(createtime, '%Y-%m-%d')  >=  ? ");
            paramList.add(paraMap.get("startDate"));
        }
        if(StringEx.isNotNull(paraMap.get("endDate"))){
            sbStr.append(" and DATE_FORMAT(createtime, '%Y-%m-%d')  <=  ? ");
            paramList.add(paraMap.get("endDate"));
        }
        if(StringEx.isNotNull(paraMap.get("status"))){
            sbStr.append(" and status = ? ");
            paramList.add(paraMap.get("status"));
        }
        if(StringEx.isNotNull(paraMap.get("bgznick"))){
            sbStr.append(" and bgznick like ? ");
            paramList.add("%"+paraMap.get("bgznick")+"%");
        }
        if(StringEx.isNotNull(paraMap.get("bgzphone"))){
            sbStr.append(" and bgzphone = ? ");
            paramList.add(paraMap.get("bgzphone"));
        }
        if(StringEx.isNotNull(paraMap.get("followednick"))){
            sbStr.append(" and followednick like ? ");
            paramList.add("%"+paraMap.get("followednick")+"%");
        }
        if(StringEx.isNotNull(paraMap.get("followedphone"))){
            sbStr.append(" and followedphone = ? ");
            paramList.add(paraMap.get("followedphone"));
        }
        sbStr.append(" order by createtime desc");
        String sql = sbStr.toString();
        return this.queryList(sql,paramList, AppFollow.class,splitPage);
    }


    /**
     * 详情
     */
    public AppFollow get(Integer id) {
        if (id==null || id<=0) {
            return null;
        }
        List<Object> paramList = new ArrayList<Object>();
        StringBuilder sbStr = new StringBuilder("select * from app_follow where id = ? ");
        paramList.add(id);
        String sql = sbStr.toString();
        return this.queryObj(sql, paramList, AppFollow.class);
    }

    /**
     * 获取两个人之间的关注
     */
    public AppFollow getFollew(Integer guanUserId,Integer beiUserId) {
        if (guanUserId==null || guanUserId<=0 || beiUserId==null || beiUserId<=0) {
            return null;
        }
        List<Object> paramList = new ArrayList<Object>();
        StringBuilder sbStr = new StringBuilder("select * from app_follow where followeduserid = ? and userid = ? ");
        paramList.add(guanUserId);
        paramList.add(beiUserId);
        String sql = sbStr.toString();
        return this.queryObj(sql, paramList, AppFollow.class);
    }

    /**
     * 添加/修改
     */
    public void save(AppFollow haoYou) {
        if (haoYou!=null && haoYou.getId() != null && haoYou.getId() > 0) {
            this.update(haoYou);
        } else {
            haoYou.setId(this.add(haoYou));
        }
    }

    /**
     * 删除
     */
    public void del(Integer id) {
        if (id==null || id <= 0) {
            return;
        }
        String sql = "delete  from app_follow where id = ?";
        ArrayList<Object> paramList = new ArrayList<Object>();
        paramList.add(id);
        this.del(sql, paramList);
    }

    public void delByUserId(Integer userId) {
        String sql = "delete from app_follow where userid = ? or followeduserid=?";
        ArrayList<Object> paramList = new ArrayList<Object>();
        paramList.add(userId);
        paramList.add(userId);
        this.del(sql, paramList);
    }

    /**
     * 查看关注人信息列表
     * @param paraMap
     * @param splitPage
     * @return
     */
    public List<AppFollow> findByList(Map<String,Object> paraMap,SplitPageBean splitPage){
        ArrayList<Object> paramList = new ArrayList<Object>();
        StringBuilder sbStr = new StringBuilder("select * from app_follow where 1=1 and  status = 1 ");
        if(StringEx.isNotNull(paraMap.get("startDate"))){
            sbStr.append(" and DATE_FORMAT(createtime, '%Y-%m-%d')  >=  ? ");
            paramList.add(paraMap.get("startDate"));
        }
        if(StringEx.isNotNull(paraMap.get("endDate"))){
            sbStr.append(" and DATE_FORMAT(createtime, '%Y-%m-%d')  <=  ? ");
            paramList.add(paraMap.get("endDate"));
        }
        if(StringEx.isNotNull(paraMap.get("userid"))){
            sbStr.append(" and userid = ?  ");
            paramList.add(paraMap.get("userid"));
        }

        if(StringEx.isNotNull(paraMap.get("followeduserid"))){
            sbStr.append(" and followeduserid = ?  ");
            paramList.add(paraMap.get("followeduserid"));
        }

        if(StringEx.isNotNull(paraMap.get("followednick"))){
            sbStr.append(" and followednick like ? ");
            paramList.add("%"+paraMap.get("followednick")+"%");
        }
        if(StringEx.isNotNull(paraMap.get("followedphone"))){
            sbStr.append(" and followedphone = ? ");
            paramList.add(paraMap.get("followedphone"));
        }
        sbStr.append(" order by createtime desc");
        String sql = sbStr.toString();
        return this.queryList(sql,paramList, AppFollow.class,splitPage);
    }

    /**
     * 查看关注人信息全部列表
     * @param paraMap
     * @return
     */
    public List<AppFollow> findByListAll(Map<String,Object> paraMap){
        ArrayList<Object> paramList = new ArrayList<Object>();
        StringBuilder sbStr = new StringBuilder("select * from app_follow where 1=1 and  status = 1 ");
        if(StringEx.isNotNull(paraMap.get("startDate"))){
            sbStr.append(" and DATE_FORMAT(createtime, '%Y-%m-%d')  >=  ? ");
            paramList.add(paraMap.get("startDate"));
        }
        if(StringEx.isNotNull(paraMap.get("endDate"))){
            sbStr.append(" and DATE_FORMAT(createtime, '%Y-%m-%d')  <=  ? ");
            paramList.add(paraMap.get("endDate"));
        }
        if(StringEx.isNotNull(paraMap.get("userid"))){
            sbStr.append(" and userid = ?  ");
            paramList.add(paraMap.get("userid"));
        }

        if(StringEx.isNotNull(paraMap.get("followeduserid"))){
            sbStr.append(" and followeduserid = ?  ");
            paramList.add(paraMap.get("followeduserid"));
        }

        if(StringEx.isNotNull(paraMap.get("followednick"))){
            sbStr.append(" and followednick like ? ");
            paramList.add("%"+paraMap.get("followednick")+"%");
        }
        if(StringEx.isNotNull(paraMap.get("followedphone"))){
            sbStr.append(" and followedphone = ? ");
            paramList.add(paraMap.get("followedphone"));
        }
        sbStr.append(" order by createtime desc");
        String sql = sbStr.toString();
        return this.queryList(sql,paramList, AppFollow.class);
    }



    /**
     * 更新关注人头像
     * @param userid
     * @param pic 头像地址
     */
    public void updateImg(Integer userid,String pic){
        if(userid==null || userid<=0 || StringEx.isNull(pic)){
            return ;
        }
        String sql = " UPDATE app_follow SET followedpic = ?  WHERE followeduserid = ? ";
        ArrayList<Object> paramList = new ArrayList<Object>();
        paramList.add(pic);
        paramList.add(userid);
        this.update(sql,paramList);
    }

    /**
     * 获取指定用户&指定用户获取关注信息
     *
     * @param userid    用户id
     * @param arr 动态id 拼接
     * @return
     */
    public List<AppFollow> getPiliang(Integer userid, String arr) {
        if(userid==null || userid<=0){
            return null;
        }
        ArrayList<Object> paramList = new ArrayList<Object>();
        StringBuilder sbStr = new StringBuilder("select * from app_follow where followeduserid = ?  ");
        paramList.add(userid);
        sbStr.append(" and userid in ( ");
        sbStr.append(arr);
        sbStr.append(" ) ");
        String sql = sbStr.toString();
        return this.queryList(sql, paramList, AppFollow.class);
    }



    /**
     * 更新关注人昵称
     * @param userid
     * @param nick 昵称
     */
    public void updateNick(Integer userid,String nick){
        if(userid==null || userid<=0 || StringEx.isNull(nick)){
            return ;
        }
        String sql = " UPDATE app_follow SET followednick = ?  WHERE followeduserid = ? ";
        ArrayList<Object> paramList = new ArrayList<Object>();
        paramList.add(nick);
        paramList.add(userid);
        this.update(sql,paramList);
    }

    /**
     * 更新被关注人头像
     * @param userid
     * @param pic 头像地址
     */
    public void updateBgzImg(Integer userid,String pic){
        if(userid==null || userid<=0 || StringEx.isNull(pic)){
            return ;
        }
        String sql = " UPDATE app_follow SET bgzpic = ?  WHERE userid = ? ";
        ArrayList<Object> paramList = new ArrayList<Object>();
        paramList.add(pic);
        paramList.add(userid);
        this.update(sql,paramList);
    }
    /**
     * 更新被关注人昵称
     * @param userid
     * @param nick 昵称
     */
    public void updateBgzNick(Integer userid,String nick){
        if(userid==null || userid<=0 || StringEx.isNull(nick)){
            return ;
        }
        String sql = " UPDATE app_follow SET bgznick = ?  WHERE userid = ? ";
        ArrayList<Object> paramList = new ArrayList<Object>();
        paramList.add(nick);
        paramList.add(userid);
        this.update(sql,paramList);
    }

    /**
    * 更新关注人性别
    * @Param: [userid, sex]
    * @return: void
    * @Author: zhouyunyi
    * @Date: 2021/07/28
    * @Time: 18:18
    */
    public void updateSex(Integer userid,Integer sex){
        if(userid==null || userid<=0 || StringEx.isNull(sex)){
            return;
        }
        String sql = "  UPDATE app_follow SET followedsex = ?  WHERE followeduserid = ? ";
        ArrayList<Object> paramList = new ArrayList<Object>();
        paramList.add(sex);
        paramList.add(userid);
        this.update(sql,paramList);
    }

    /**
     * 更新被关注人性别
     * @Param: [userid, sex]
     * @return: void
     * @Author: zhouyunyi
     * @Date: 2021/07/28
     * @Time: 18:18
     */
    public void updateBgzSex(Integer userid,Integer sex){
        if(userid==null || userid<=0 || StringEx.isNull(sex)){
            return;
        }
        String sql = " UPDATE app_follow SET bgzsex = ?  WHERE userid  = ? ";
        ArrayList<Object> paramList = new ArrayList<Object>();
        paramList.add(sex);
        paramList.add(userid);
        this.update(sql,paramList);
    }


    /**
    * 更新关注人签名
    * @Param: [userid, mysign]
    * @return: void
    * @Author: zhouyunyi
    * @Date: 2021/07/28
    * @Time: 19:17
    */
    public void updateMysign(Integer userid,String mysign){
        if(userid==null || userid<=0 || StringEx.isNull(mysign)){
            return;
        }
        String sql = " UPDATE app_follow SET followedmysign = ?  WHERE followeduserid = ? ";
        ArrayList<Object> paramList = new ArrayList<Object>();
        paramList.add(mysign);
        paramList.add(userid);
        this.update(sql,paramList);
    }

    /**
    * 更新被关注人签名
    * @Param: [userid, mysign]
    * @return: void
    * @Author: zhouyunyi
    * @Date: 2021/07/28
    * @Time: 19:18
    */
    public void updateBgzMysign(Integer userid,String mysign){
        if(userid==null || userid<=0 || StringEx.isNull(mysign)){
            return;
        }
        String sql = " UPDATE app_follow SET bgzmysign = ?  WHERE userid = ? ";
        ArrayList<Object> paramList = new ArrayList<Object>();
        paramList.add(mysign);
        paramList.add(userid);
        this.update(sql,paramList);
    }

    public List<AppFollow> selectALL(Integer id) {
        StringBuilder str = new StringBuilder();
        List list = new ArrayList();
        str.append("select * from app_follow where followeduserid = ? ");
        list.add(id);
        String sql = str.toString();
        return this.queryList(sql,list,AppFollow.class);
    }
    /**
     * 获取一天内的粉丝数量
     *
     * @return
     */
    public List<AppFollow> getNewFansByDay (){
        StringBuilder str = new StringBuilder();
        List list = new ArrayList();
        str.append("select count(1) as fansNum ,bgzsex,bgzphone,bgznick, bgzpic,bgzgrade,bgzusercode,bgzmysign,followeduserid  from app_follow where createTime   BETWEEN   DATE_SUB(current_timestamp,INTERVAL  1 DAY)    and  current_timestamp  and status = 1  group by bgzusercode order by fansNum desc limit 50 ");

        String sql = str.toString();
        return  this.queryList(sql,list,AppFollow.class);
    }

    /**
     * 获取7天内的粉丝数量
     *
     * @return
     */
    public List<AppFollow> getNewFansBySevenDay (){
        StringBuilder str = new StringBuilder();
        List list = new ArrayList();
        str.append("select count(1) as fansNum ,bgzsex,bgzphone,bgznick, bgzpic,bgzgrade,bgzusercode,bgzmysign,followeduserid  from app_follow where createTime   BETWEEN   DATE_SUB(current_timestamp,INTERVAL  1 WEEK)    and  current_timestamp   and status = 1 group by bgzusercode order by fansNum desc limit 50 ");
        String sql = str.toString();
        return  this.queryList(sql,list,AppFollow.class);
    }

    /**
     * 查询用户的粉丝总数
     * @param userIds
     * @return
     */
    public List<AppFollow> queryFollowSum(String userIds) {
        ArrayList<Object> paramList = new ArrayList<Object>();
        StringBuilder sbStr = new StringBuilder("select * from app_follow where status='1'");
            sbStr.append(" and userid in ( ");
            sbStr.append(userIds);
            sbStr.append(" ) ");
        String sql = sbStr.toString();
        return this.queryList(sql,paramList, AppFollow.class);
    }

    public List<AppFollow> queryFollowSum(Integer userId) {
        ArrayList<Object> paramList = new ArrayList<Object>();
        StringBuilder sbStr = new StringBuilder("select * from app_follow where status='1'");
        sbStr.append(" and userid =  "+ userId);
        String sql = sbStr.toString();
        return this.queryList(sql,paramList, AppFollow.class);
    }
}
